<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="~assets/images/logo.png" alt="" />
      </div>
      <!-- 表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
        @keyup.enter.native="login"
      >
        <el-form-item prop="userName">
          <el-input
            prefix-icon="iconfont icon-username"
            v-model="loginForm.username"
            placeholder="请输入用户名"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="userPassword">
          <el-input
            prefix-icon="iconfont icon-mima"
            type="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
          >
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login"
            >登录</el-button
          >
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '用户名长度在3-10位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 16, message: '密码长度为5-16位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 登录前验证，不合法返回false，不发送请求
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          this.$message.error('用户名或密码长度错误')
        } else {
          await this.axios
            .post('login', this.loginForm)
            .then(res => {
              const data = res.data
              if (data.meta.status === 200) {
                this.$message({
                  message: '登录成功！',
                  type: 'success'
                })
                window.sessionStorage.setItem('token', data.data.token)
                this.$router.push('/home')
              } else {
                this.$message.error('用户名或密码错误')
              }
            })
            .catch(err => {
              this.$message.error(err)
            })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    .avatar_box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      background-color: #fff;
      // 根据自身的高度向上偏移50%
      transform: translate(0, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .login_form {
      width: 350px;
      position: absolute;
      transform: translate(0, 54%);
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
